package com.ys.controller.web.eKnowAi;

import com.ys.entity.eKnowAi.EKnowKnowledgeBase;
import com.ys.entity.eKnowAi.EKnowKnowledgeType;
import com.ys.entity.eKnowAi.EKnowScene;
import com.ys.entity.eKnowAi.EKnowTemplateType;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/web/eKnowAi/webEKnowAiController")
public class WebEKnowAiController {
	private static final Logger logger = LoggerFactory.getLogger(WebEKnowAiController.class);

	//知识库新增、修改页面
	@GetMapping("/knowledgeBaseAdd")
	public String knowledgeBaseAdd(Model model,String knowledgeBaseId,String addOrUpdate){


		if(addOrUpdate.equals("update") && knowledgeBaseId!=null && !"".equals(knowledgeBaseId)){
			EKnowKnowledgeBase eKnowKnowledgeBase = new EKnowKnowledgeBase().setId(Long.parseLong(knowledgeBaseId)).queryFirst();
			ToKnowledgeBaseAdd dto = new ToKnowledgeBaseAdd(addOrUpdate,knowledgeBaseId);
			model.addAttribute("dto", dto);
		}else {
			EKnowKnowledgeBase eKnowKnowledgeBase = new EKnowKnowledgeBase().setId(Long.parseLong(knowledgeBaseId)).queryFirst();
			ToKnowledgeBaseAdd dto = new ToKnowledgeBaseAdd(addOrUpdate,null);
			model.addAttribute("dto", dto);
		}

		return "eKnowAi/knowledgeBasePage/knowledgeBaseAdd";
	}

	@Data
	@AllArgsConstructor
	private static class ToKnowledgeBaseAdd{
		private String addOrUpdate;
		private String knowledgeBaseId;

	}

	//知识库资料页面
	@GetMapping("/knowledgeBaseList")
	public String knowledgeBaseList(Model model,String id){


		List<Map<String,Object>> conditions = new ArrayList<>();
		Map<String,Object> searchMsg2 = new HashMap<>();
		searchMsg2.put("type","t_dropdown");
		searchMsg2.put("code","A.TypeId");
		searchMsg2.put("rangeType","t_system");
		searchMsg2.put("value",null);
		searchMsg2.put("name","分类");
		searchMsg2.put("placeHolder","模糊匹配");
		List<Map<String,Object>> optionArr = new ArrayList<>();
		Map<String,Object> option = new HashMap<>();
		option.put("value","");
		option.put("name","请选择");
		optionArr.add(option);
		List<EKnowKnowledgeType> eKnowKnowledgeTypeList = new EKnowKnowledgeType().setBaseId(Long.parseLong(id)).queryList();
		if(eKnowKnowledgeTypeList!=null && eKnowKnowledgeTypeList.size()>0){
			for(EKnowKnowledgeType item:eKnowKnowledgeTypeList){
				Map<String,Object> optionType = new HashMap<>();
				optionType.put("value",item.getId());
				optionType.put("name",item.getTypeName());
				optionArr.add(optionType);
			}
		}
		searchMsg2.put("optionArr",optionArr);
		conditions.add(searchMsg2);
		Map<String,Object> searchMsg1 = new HashMap<>();
		searchMsg1.put("type","t_text");
		searchMsg1.put("code","information");
		searchMsg1.put("rangeType","t_bothlike");
		searchMsg1.put("value",null);
		searchMsg1.put("name","文档内容");
		searchMsg1.put("placeHolder","模糊匹配");
		conditions.add(searchMsg1);


		EKnowKnowledgeBase eKnowKnowledgeBase = new EKnowKnowledgeBase().setId(Long.parseLong(id)).queryFirst();
		ToKnowledgeList dto = new ToKnowledgeList(id,eKnowKnowledgeBase.getKnowledgeBaseName(),conditions);
		model.addAttribute("dto", dto);
		return "eKnowAi/knowledgeBasePage/knowledgeList";
	}

	@Data
	@AllArgsConstructor
	private static class ToKnowledgeList{
		private String id;
		private String name;
		private List<Map<String,Object>> conditions;
	}


	//知识库资料页新增页面
	@GetMapping("/knowledgeBaseListAdd")
	public String knowledgeBaseListAdd(Model model,String id){

		//获取分类信息
		List<EKnowKnowledgeType> eKnowKnowledgeTypeList = new EKnowKnowledgeType().setBaseId(Long.parseLong(id)).queryList();

		ToKnowledgeListAdd dto = new ToKnowledgeListAdd(id,eKnowKnowledgeTypeList);
		model.addAttribute("dto", dto);
		return "eKnowAi/knowledgeBasePage/knowledgeListAdd";
	}

	@Data
	@AllArgsConstructor
	private static class ToKnowledgeListAdd{
		private String id;
		private List<EKnowKnowledgeType> eKnowKnowledgeTypeList;
	}

	//知识库资料页修改页面
	@GetMapping("/knowledgeBaseListUpdate")
	public String knowledgeBaseListUpdate(Model model,String id,String sectionId){
		ToKnowledgeListUpdate dto = new ToKnowledgeListUpdate(id,sectionId,null);
		model.addAttribute("dto", dto);
		return "eKnowAi/knowledgeBasePage/knowledgeListUpdate";
	}

	//知识库资料页修改页面
	@GetMapping("/knowledgeListUpdateBySection")
	public String knowledgeListUpdateBySection(Model model,String id,String sectionId,String baseId){
		ToKnowledgeListUpdate dto = new ToKnowledgeListUpdate(id,sectionId,baseId);
		model.addAttribute("dto", dto);
		return "eKnowAi/knowledgeBasePage/knowledgeListUpdateBySection";
	}

	//知识库资料页详情页面
	@GetMapping("/knowledgeBaseListDetail")
	public String knowledgeBaseListDetail(Model model,String id,String sectionId){
		ToKnowledgeListUpdate dto = new ToKnowledgeListUpdate(id,sectionId,null);
		model.addAttribute("dto", dto);
		return "eKnowAi/knowledgeBasePage/knowledgeListDetail";
	}

	@Data
	@AllArgsConstructor
	private static class ToKnowledgeListUpdate{
		private String id;
		private String sectionId;
		private String baseId;
	}


	//知识库资料页分类管理页面
	@GetMapping("/knowledgeBaseListType")
	public String knowledgeBaseListType(Model model,String id){
		//获取分类信息
		ToPageWithId dto = new ToPageWithId(id);
		model.addAttribute("dto", dto);
		return "eKnowAi/knowledgeBasePage/knowledgeListType";
	}


	//定时表单分类绑定-修改
	@GetMapping("/eKnowTimerSetUpdate")
	public String eKnowTimerSetUpdate(Model model,String id){
		//获取分类信息
		ToPageWithId dto = new ToPageWithId(id);
		model.addAttribute("dto", dto);
		return "eKnowAi/timerSet/eKnowTimerSetUpdate";
	}



	//知识库资料页上传页面
	@GetMapping("/knowledgeBaseListUpload")
	public String knowledgeBaseListUpload(Model model,String baseId){
		ToPageWithId dto = new ToPageWithId(baseId);
		model.addAttribute("dto", dto);
		return "eKnowAi/knowledgeBasePage/knowledgeListUpload";
	}

	@Data
	@AllArgsConstructor
	private static class ToPageWithId{
		private String id;
	}




	//提示词模版页面
	@GetMapping("/eknowTemplateList")
	public String eknowTemplateList(Model model,String id){


		List<Map<String,Object>> conditions = new ArrayList<>();
		Map<String,Object> searchMsg2 = new HashMap<>();
		searchMsg2.put("type","t_dropdown");
		searchMsg2.put("code","A.TypeId");
		searchMsg2.put("rangeType","t_system");
		searchMsg2.put("value",null);
		searchMsg2.put("name","分类");
		searchMsg2.put("placeHolder","模糊匹配");
		List<Map<String,Object>> optionArr = new ArrayList<>();
		Map<String,Object> option = new HashMap<>();
		option.put("value","");
		option.put("name","请选择");
		optionArr.add(option);
		List<EKnowTemplateType> eKnowTemplateTypeList = new EKnowTemplateType().setSceneId(Long.parseLong(id)).queryList();
		if(eKnowTemplateTypeList!=null && eKnowTemplateTypeList.size()>0){
			for(EKnowTemplateType item:eKnowTemplateTypeList){
				Map<String,Object> optionType = new HashMap<>();
				optionType.put("value",item.getId());
				optionType.put("name",item.getTypeName());
				optionArr.add(optionType);
			}
		}
		searchMsg2.put("optionArr",optionArr);
		conditions.add(searchMsg2);
		Map<String,Object> searchMsg1 = new HashMap<>();
		searchMsg1.put("type","t_text");
		searchMsg1.put("code","templateName");
		searchMsg1.put("rangeType","t_bothlike");
		searchMsg1.put("value",null);
		searchMsg1.put("name","提示词标题");
		searchMsg1.put("placeHolder","模糊匹配");
		conditions.add(searchMsg1);


		EKnowScene eKnowScene = new EKnowScene().setId(Long.parseLong(id)).queryFirst();
		ToKnowledgeList dto = new ToKnowledgeList(id,eKnowScene.getSceneName(),conditions);
		model.addAttribute("dto", dto);
		return "eKnowAi/prompt/templateList";
	}


	//提示词模版新增页面
	@GetMapping("/templateListAdd")
	public String templateListAdd(Model model,String id){

		//获取分类信息
		List<EKnowTemplateType> eKnowTemplateTypeList = new EKnowTemplateType().setSceneId(Long.parseLong(id)).queryList();

		ToTemplateListAdd dto = new ToTemplateListAdd(id,eKnowTemplateTypeList);
		model.addAttribute("dto", dto);
		return "eKnowAi/prompt/templateListAdd";
	}

	@Data
	@AllArgsConstructor
	private static class ToTemplateListAdd{
		private String id;
		private List<EKnowTemplateType> eKnowTemplateTypeList;
	}

	//提示词模版修改页面
	@GetMapping("/templateListUpdate")
	public String templateListUpdate(Model model,String id){
		ToPageWithId dto = new ToPageWithId(id);
		model.addAttribute("dto", dto);
		return "eKnowAi/prompt/templateListUpdate";
	}

	//提示词模版详情页面
	@GetMapping("/templateListDetail")
	public String templateListDetail(Model model,String id,String sectionId){
		ToPageWithId dto = new ToPageWithId(id);
		model.addAttribute("dto", dto);
		return "eKnowAi/prompt/templateListDetail";
	}

	//提示词模版页分类管理页面
	@GetMapping("/templateListType")
	public String templateListType(Model model,String id){
		//获取分类信息
		ToPageWithId dto = new ToPageWithId(id);
		model.addAttribute("dto", dto);
		return "eKnowAi/prompt/templateListType";
	}

	//日志查看-详情
	@GetMapping("/chatLogDetail")
	public String chatLogDetail(Model model,String id){
		ToPageWithId dto = new ToPageWithId(id);
		model.addAttribute("dto", dto);
		return "eKnowAi/chatLog/chatLogDetail";
	}


	//日志管理-上传日志到知识库页面
	@GetMapping("/chatLogManageUpload")
	public String chatLogManageUpload(Model model,String id){
		ToPageWithId dto = new ToPageWithId(id);
		model.addAttribute("dto", dto);
		return "eKnowAi/chatLog/chatLogManageUpload";
	}


	//日志管理-修改
	@GetMapping("/chatLogManageUpdate")
	public String chatLogManageUpdate(Model model,String id){
		ToPageWithId dto = new ToPageWithId(id);
		model.addAttribute("dto", dto);
		return "eKnowAi/chatLog/chatLogManageUpdate";
	}

	//日志管理-详情
	@GetMapping("/chatLogManageDetail")
	public String chatLogManageDetail(Model model,String id){
		ToPageWithId dto = new ToPageWithId(id);
		model.addAttribute("dto", dto);
		return "eKnowAi/chatLog/chatLogManageDetail";
	}

	//场景信息-修改
	@GetMapping("/sceneUpdate")
	public String sceneUpdate(Model model,String id){
		ToPageWithId dto = new ToPageWithId(id);
		model.addAttribute("dto", dto);
		return "eKnowAi/scene/sceneUpdate";
	}

	//场景信息-新增
	@GetMapping("/sceneAdd")
	public String sceneAdd(Model model){

		return "eKnowAi/scene/sceneAdd";
	}






	//以下是未开发
	@GetMapping("/modelCorpusAdd")
	public String modelCorpusAdd(){

		return "eKnowAi/modelCorpusAdd";
	}

	@GetMapping("/modelCorpusListAdd")
	public String modelCorpusListAdd(){

		return "eKnowAi/modelCorpusListAdd";
	}


	@GetMapping("/modelCorpusListUpload")
	public String modelCorpusListUpload(){

		return "eKnowAi/modelCorpusListUpload";
	}

	@GetMapping("/promptProjectAdd")
	public String promptProjectAdd(){

		return "eKnowAi/promptProjectAdd";
	}


	@GetMapping("/modelCorpusList")
	public String modelCorpusList(){

		return "eKnowAi/modelCorpusList";
	}
}



